package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.UserFeedback;
import com.example.mall.dto.UserFeedbackDto;
import com.example.mall.mapper.UserFeedbackMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserFeedbackQuery;
import com.example.mall.service.IUserFeedbackService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户反馈表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements IUserFeedbackService {

    @Autowired
    private UserFeedbackMapper userFeedbackMapper;

    @Override
    public PageDTO<UserFeedbackDto> queryPage(UserFeedbackQuery userFeedbackQuery) {
        if (userFeedbackQuery == null) {
            userFeedbackQuery = new UserFeedbackQuery();
        }
        Page<UserFeedback> page = userFeedbackQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserFeedback> cartPage = lambdaQuery()
                .like(userFeedbackQuery.getFeedbackId()!= null, UserFeedback::getFeedbackId, userFeedbackQuery.getFeedbackId())
                .like(userFeedbackQuery.getUserId()!= null, UserFeedback::getUserId, userFeedbackQuery.getUserId())
                .like(userFeedbackQuery.getFeedbackType()!= null, UserFeedback::getFeedbackType, userFeedbackQuery.getFeedbackType())
                .like(userFeedbackQuery.getContent()!= null, UserFeedback::getContent, userFeedbackQuery.getContent())
                .like(userFeedbackQuery.getStatus()!= null, UserFeedback::getStatus, userFeedbackQuery.getStatus())
                .orderByDesc(UserFeedback::getUpdateTime)
                .page(page);
        return PageDTO.of(cartPage, UserFeedbackDto.class);
    }

    @Override
    public UserFeedback queryUserFeedbackById(Long feedbackId) {
        AssertUtil.isNotNull(feedbackId, "feedbackId不能为空");
        return userFeedbackMapper.selectById(feedbackId);
    }

    @Override
    public List<UserFeedback> queryUserFeedbackList() {
        return this.list();
    }

    @Override
    public void insert(UserFeedbackDto userFeedbackDto) {
        UserFeedback userFeedback = new UserFeedback();
        CopyUtils.copyProperties(userFeedbackDto, userFeedback);
        userFeedback.setFeedbackId(CodeGenerateUtils.generateUniqueId());
        userFeedback.setCreateDate(new Date());
        userFeedback.setUpdateTime(new Date());
        userFeedback.setStatus(BaseConstants.EFFECTIVE_STATUS);
        userFeedbackMapper.insert(userFeedback);
    }

    @Override
    public void update(UserFeedbackDto userFeedbackDto) {
        AssertUtil.isNotNull(userFeedbackDto.getFeedbackId(), "feedbackId不能为空");
        UserFeedback userFeedback = userFeedbackMapper.selectById(userFeedbackDto.getFeedbackId());
        AssertUtil.isNotNull(userFeedback, "feedbackId不存在");
        CopyUtils.copyProperties(userFeedbackDto, userFeedback);
        userFeedback.setUpdateTime(new Date());
        userFeedbackMapper.updateById(userFeedback);
    }

    @Override
    public void delete(Long feedbackId) {
        AssertUtil.isNotNull(feedbackId, "feedbackId不能为空");
        UserFeedback userFeedback = userFeedbackMapper.selectById(feedbackId);
        AssertUtil.isNotNull(userFeedback, "feedbackId不存在");
        userFeedback.setStatus(BaseConstants.LOSE_STATUS);
        userFeedback.setUpdateTime(new Date());
        userFeedbackMapper.updateById(userFeedback);
    }

    @Override
    public void deleteBatch(List<Long> feedbackIds) {
        AssertUtil.isNotNull(feedbackIds, "feedbackIds不能为空");
        List<UserFeedback> userFeedbacks = userFeedbackMapper.selectBatchIds(feedbackIds);
        AssertUtil.isNotNull(userFeedbacks, "feedbackIds不存在");
        for (UserFeedback userFeedback : userFeedbacks) {
            userFeedback.setStatus(BaseConstants.LOSE_STATUS);
            userFeedback.setUpdateTime(new Date());
            userFeedbackMapper.updateById(userFeedback);
        }
    }
}
